
(function($){
	
	// A global array used by the functions of the plug-in:
	var gVars = {};

	// Extending the jQuery core:
	$.fn.tzineClock = function(opts){
	
		var container = this.eq(0);
	
		if(!container)
		{
			try{
				console.log("Invalid selector!");
			} catch(e){}
			
			return false;
		}
		
		if(!opts) opts = {}; 
		
		var defaults = {
			time: 10,
			onTick: function() {
			},
			onFinish: function() {
			},
			onStart: function() {
			}
		};
		
		/* Merging the provided options with the default ones (will be used in future versions of the plugin): */
		$.each(defaults,function(k,v){
			opts[k] = opts[k] || defaults[k];
		})

		// Calling the setUp function and passing the container,
		// will be available to the setUp function as "this":
		setUp.call(container, opts);
		
		return this;
	}
	
	function setUp(opts)
	{
		if($(".display", this).length) {
			gVars.orange.maxTime = opts.time;
			gVars.orange.currentTime = opts.time;
			gVars.orange.active = false;
			gVars.orange.onTick = opts.onTick;
			gVars.orange.onStart = opts.onStart;
			gVars.orange.onFinish = opts.onFinish;
			return;
		}
		// The colors of the dials:
		var colors = ['orange','blue','green'];
		
		var tmp;
		
		for(var i=0;i<1;i++)
		{
			// Creating a new element and setting the color as a class name:
			
			tmp = $('<div>').attr('class',colors[i]+' clock').html(
				'<div class="display"></div>'+
				
				'<div class="rotate left">'+
					'<div class="bg left"></div>'+
				'</div>'+
				
				'<div class="front right"></div>'+
				
				'<div class="rotate right">'+
					'<div class="bg right"></div>'+
				'</div>'
			);
			
			// Appending to the container:
			$(this).append(tmp);
			
			// Assigning some of the elements as variables for speed:
			tmp.rotateLeft = tmp.find('.rotate.left');
			tmp.rotateRight = tmp.find('.rotate.right');
			tmp.display = tmp.find('.display');
			tmp.currentTime = opts.time;
			tmp.maxTime = opts.time;
			tmp.active = false;
			tmp.onTick = opts.onTick;
			tmp.onStart = opts.onStart;
			tmp.onFinish = opts.onFinish;
			
			// Adding the dial as a global variable. Will be available as gVars.colorName
			gVars[colors[i]] = tmp;
		}
		
		// Setting up a interval, executed every 1000 milliseconds:
		setInterval(function(){
			animation(gVars.orange, gVars.orange.currentTime, gVars.orange.maxTime);
		},1000);
	}
	
	function animation(clock, current, total)
	{
		if(!clock.active)
			return;
		
		if(current == total) {
			gVars.orange.onStart();
		}
		else {
			gVars.orange.onTick();
		}
		
		// Calculating the current angle:
		var angle = (360/total)*(total - current);
	
		var element;
		
		if(current == total) {
			clock.rotateRight.show();
			clock.rotateLeft.show();
			rotateElement(clock.rotateLeft,0);
			rotateElement(clock.rotateRight,0);
		}
	
		if(angle<=180)
		{
			// The left part is rotated, and the right is currently hidden:
			element = clock.rotateRight;
		}
		else if(clock.rotateRight.is(":visible")) {
			clock.rotateRight.hide();
			clock.rotateLeft.show();
			rotateElement(clock.rotateLeft,0);
			element = clock.rotateLeft;
			angle = angle - 180;
		}
		else
		{
			element = clock.rotateLeft;
			angle = angle - 180;
		}

		rotateElement(element,angle);
		
		// Setting the text inside of the display element, inserting a leading zero if needed:
		clock.display.html(current);
		clock.currentTime -= 1;
		if(clock.currentTime < 0) {
			$.stopClock();
			gVars.orange.onFinish();
		}
	}
	
	function rotateElement(element,angle)
	{
		// Rotating the element, depending on the browser:
		var rotate = 'rotate('+angle+'deg)';
		
		if(element.css('MozTransform')!=undefined)
			element.css('MozTransform',rotate);
			
		else if(element.css('WebkitTransform')!=undefined)
			element.css('WebkitTransform',rotate);
	
		// A version for internet explorer using filters, works but is a bit buggy (no surprise here):
		else if(element.css("filter")!=undefined)
		{
			var cos = Math.cos(Math.PI * 2 / 360 * angle);
			var sin = Math.sin(Math.PI * 2 / 360 * angle);
			
			element.css("filter","progid:DXImageTransform.Microsoft.Matrix(M11="+cos+",M12=-"+sin+",M21="+sin+",M22="+cos+",SizingMethod='auto expand',FilterType='nearest neighbor')");
	
			element.css("left",-Math.floor((element.width()-200)/2));
			element.css("top",-Math.floor((element.height()-200)/2));
		}
	
	}
	
	$.startClock = function() {
		gVars.orange.active = true;
	};
	
	$.stopClock = function() {
		gVars.orange.active = false;
	};
	
	$.getClockTime = function() {
		return gVars.orange.currentTime;
	};
	
})(jQuery)